package com.itstyle.seckill.web;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import com.sun.deploy.association.utility.AppConstants;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/logger")
public class LogController {
    /**
     * 测试日志等级
     * @return
     */
    @RequestMapping("/loglevel")
    public String logLevel(){
        log.debug("debug log");
        log.info("info log");
        log.error("error log");
        return "success";
    }
    /**
     * 设置日志等级
     * @param level
     * @return
     */
    @RequestMapping("/changeLog/{level}")
    public String changeLevel(@PathVariable String level){
        LoggerContext loggerContext= (LoggerContext) LoggerFactory.getILoggerFactory();
        List<Logger> loggerList = loggerContext.getLoggerList();
        for(Logger logger : loggerList){
            logger.setLevel(Level.toLevel(level));
        }
        return "sucess";
    }
}
